package cn.tedu.controller;

import cn.tedu.pojo.Dept;
import cn.tedu.pojo.Role;
import cn.tedu.pojo.User;
import cn.tedu.service.DeptService;
import cn.tedu.service.RoleService;
import cn.tedu.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/sysadmin/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;

    @RequestMapping("/list")
    public String list(Model model) {
        List<User> users = userService.findAll();
        model.addAttribute("users", users);
        return "/sysadmin/user/UserList";
    }

    //点击禁用实现通用功能
    @RequestMapping("/stop1")
    public String stop(Model model, @RequestParam(value = "userId", required = false) String[] userIds) {
        if (userIds != null) {

            userService.changeState(0, userIds);

        }
        //重定向当前页面
        return "redirect:/sysadmin/user/list";
    }

    //点击启用实现通用功能
    @RequestMapping("/start1")
    public String start(Model model, @RequestParam(value = "userId", required = false) String[] userIds) {
        if (userIds != null) {
            userService.changeState(1, userIds);
        }
        //重定向当前页面
        return "redirect:/sysadmin/user/list";
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam(value = "userId", required = false) String[] userIds) {
        if (userIds != null) {
            userService.delete(userIds);
        }
        return "redirect:/sysadmin/user/list";
    }

    //跳转到新增页面
    @RequestMapping("/tocreate")
    public String create(Model model) {
        //得到所有的部门
        List<Dept> depts = deptService.findAll();
        model.addAttribute("depts", depts);
        //得到所有用户
        model.addAttribute("users", userService.findAll());
        return "/sysadmin/user/UserCreate";
    }

    @RequestMapping("/save")
    public String save(User user) {

        userService.saveUser(user);

        return "redirect:/sysadmin/user/list";
    }

    @RequestMapping("/toupdate")
    public String toupdate(Model model, String userId) {

        User user = userService.findOneById(userId);

        List<Dept> depts = deptService.findAll();

        List<User> users = userService.findAll();

        model.addAttribute("user", user);
        model.addAttribute("users", users);
        model.addAttribute("depts", depts);

        return "/sysadmin/user/jUserUpdate";
    }


    @RequestMapping("/update")
    public String update(User user) {
        userService.update(user);
        return "redirect:/sysadmin/user/list";
    }

    @RequestMapping("/role")
    public String role(Model model, String userId) throws JsonProcessingException {
        //查找所有的角色
        List<Role> roles = roleService.findAll();
        //查询和用户相关的所有角色
        List<String> roleIds = roleService.findRoleByUserId(userId);
        for (Role role: roles
             ) {
            for (String roleId: roleIds
                 ) {
                if(role.getId().equals(roleId)){
                    role.setChecked(true);
                }
            }
        }
        ObjectMapper objectMapper = new ObjectMapper();
        //把所有角色的集合 转化为json字符串
        String jsonString = objectMapper.writeValueAsString(roles);
        System.out.println(jsonString);
        model.addAttribute("jsonString", jsonString);
        //将用户的userid传递进去
        model.addAttribute("id",userId);
        return "/sysadmin/user/jUserRole";
    }

    @RequestMapping("/saveUserRole")
    public String saveUserRole(Model model,String userId,String[] roleIds) {
        userService.saveUserRole(userId,roleIds);
        return "redirect:/sysadmin/user/list";
    }
}
